package com.xiaoyu.client.ui.activity.main.mine.set_favorable;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.xiaoyu.client.R;
import com.xiaoyu.client.model.mine.AddressBean;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.client.ui.activity.mall.OrderComfirmActivity;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.adapter.AllPowerfulAdapter;
import com.xiaoyu.commonlib.utils.ToastUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.app.Activity.RESULT_OK;

/**
 * @author jx
 * @date 创建时间 2018/10/15
 * @Description 我的优惠券
 */
@SuppressLint("ValidFragment")
public class AvailableFragment extends Fragment {
    public static final String COUPON_CHOOSE = "coupon choose";
    private int type;
    private View mView;
    private List<AddressBean.DataBean> mAvailableList = new ArrayList<>();
    private AllPowerfulAdapter mMyAdapter;
    @BindView(R.id.common_recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.smart_refresh_layout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.common_recycler_view_empty_page)
    ImageView mEmptyPage;

    private double money = -1;

    public AvailableFragment(int type) {
        this.type = type;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null){
            money = bundle.getDouble(OrderComfirmActivity.ORDER_MONEY);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(R.layout.common_recycler_view, container, false);
            //绑定fragment
            ButterKnife.bind(this, mView);
            initView();
        }
        return mView;
    }

    private void initView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        smartRefreshLayout.setEnableRefresh(false);
        smartRefreshLayout.setEnableLoadMore(false);

        if (type == 1) {//可用
            getAvailbleList("1");
        }
        if (type == 2) {//已使用
            getAvailbleList("2");
        }
        if (type == 3) {//已过期
            getAvailbleList("3");
        }
    }

    private void getAvailbleList(final String usercouponsstate) {
        NetworkManager.myCoupons(usercouponsstate, new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                succeedResult(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {

            }
        });
    }

    private void succeedResult(String result) {
        AddressBean bean = new Gson().fromJson(result, AddressBean.class);
        mAvailableList.addAll(bean.getData());
        if (bean.getData().size() == 0) {
            mEmptyPage.setVisibility(View.VISIBLE);
            if (type == 1) {
                mEmptyPage.setImageResource(R.mipmap.mine_icon_wu);
            } else if (type == 2) {
                mEmptyPage.setImageResource(R.mipmap.mine_icon_yishiyong);
            } else {
                mEmptyPage.setImageResource(R.mipmap.mine_icon_guoqi);
            }
            return;
        }

        setMyAdapter(mAvailableList);
    }

    private void setMyAdapter(final List<AddressBean.DataBean> mAvailableList) {
        mMyAdapter = new AllPowerfulAdapter<AddressBean.DataBean>(R.layout.fragment_available, mAvailableList, new AllPowerfulAdapter.OnClickListener<AddressBean.DataBean>() {
            @Override
            public void setOnClick(BaseViewHolder baseViewHolder, AddressBean.DataBean dataBean, int position) {
                double priceLimit = Double.parseDouble(mAvailableList.get(position).getCouponsachieve());
                if (type == 1 && compareDouble(money,priceLimit) >= 0){
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(COUPON_CHOOSE,mAvailableList.get(position));
                    intent.putExtras(bundle);
                    getActivity().setResult(RESULT_OK,intent);
                    getActivity().finish();
                }
            }
        }) {
            @Override
            protected void convert(BaseViewHolder baseViewHolder, AddressBean.DataBean dataBean) {
                super.convert(baseViewHolder, dataBean);

                baseViewHolder.setText(R.id.fragment_available_money, dataBean.getCouponsmoney());
                baseViewHolder.setText(R.id.fragment_available_discount_coupon, dataBean.getCouponsname());
                baseViewHolder.setText(R.id.fragment_available_achieve, "满" + dataBean.getCouponsachieve() + "元使用");

                ImageView mAvailableImg = baseViewHolder.getView(R.id.fragment_available_img);


                if (dataBean.getUsercouponsstate().equals("1")) {//1=未使用
                    mAvailableImg.setImageResource(R.mipmap.mine_icon_youhuijuan);
                } else if (dataBean.getUsercouponsstate().equals("2")) {//2=已使用
                    mAvailableImg.setImageResource(R.mipmap.mine_icon_youhuijuan_yishiyong);
                } else if(dataBean.getUsercouponsstate().equals("3")){//3==已过期
                    mAvailableImg.setImageResource(R.mipmap.mine_icon_youhuijuan_yiguoqi);
                }
            }
        };
        mRecyclerView.setAdapter(mMyAdapter);
    }


    /**
     * 比较double类型的大小
     * @return -1:第二个数大  0：大小一样  1：第一个数大
     */
    private int compareDouble(double var1,double var2){
        BigDecimal data1 = new BigDecimal(var1);
        BigDecimal data2 = new BigDecimal(var2);
        if (data1.compareTo(data2) < 0) {
            return -1;
        }else if (data1.compareTo(data2) == 0) {
            return 0;
        }else {
            return 1;
        }
    }

}
